package controllers.listeners.feature;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import controllers.shared.GetInputLoop;

import objects.Feature;

import views.ViewFeatures;

/* Adds a value to the feature selected in the table */
/* actionPerformed when you click the "Value" button in the Features Module */
public class AddFeatureValueActionListener implements ActionListener {
	
	private ViewFeatures view;
	
	public AddFeatureValueActionListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		Feature feature = null;
		
		if(view.getSelectedFeatureType().equals("Semantic Rep")) {
			feature = view.getSelectedFeature();
		} else if(view.getSelectedFeatureType().equals("Lexicon")) {
			feature = view.getSelectedFeatureLexicon();
		}
		
		String input = null;
		
		if (feature == null) {
			JOptionPane.showMessageDialog(view, "Please select a feature from the table.", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		/* Get feature value */
		GetInputLoop.setInputDialog("Enter the new value of '" + feature.getName() + "'.", "Add Value", JOptionPane.OK_CANCEL_OPTION);
		GetInputLoop.setErrorDialog("Please enter a default value.", "Error");
		input = GetInputLoop.getInput();
		
		if (input == null) {
			return;
		}
		
		feature.addValueListItem(input);
		
		if(view.getSelectedFeatureType().equals("Semantic Rep")) {
			view.loadConstituent(view.getCurrentConstituent());
		}
		else if(view.getSelectedFeatureType().equals("Lexicon")) {
			view.loadLexicon(view.getCurrentLexicon());
		}
	}
	
	public void setView(ViewFeatures view) {
		this.view = view;
	}
	
}
